package com.ruoyi.web.controller.water;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.waterEnity.OrderEntity;
import com.ruoyi.system.domain.waterEnity.OrderItemEntity;
import com.ruoyi.system.domain.waterEnity.UserEntity;
import com.ruoyi.system.service.waterService.OrderService;
import com.ruoyi.system.service.waterService.ProductService;
import com.ruoyi.system.service.waterService.UserService;
import net.sf.json.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@Controller
@RequestMapping("/water")
public class OrderController extends BaseController {
    String prefix = "water/order";
    @Autowired
    private ProductService productService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;



    /**
     * 跳转到订单页
     * @return
     */
    @RequiresPermissions("water:order:view")
    @GetMapping("/order")
    public String order(){
        return prefix+"/order";
    }
    /**
     * 订单列表
     *
     * @return
     */
    @RequiresPermissions("water:order:view")
    @PostMapping("/orderList")
    @ResponseBody
    public TableDataInfo list(String order_id,String pay_state,String state) {
        startPage();
        List<OrderEntity> list = orderService.selectOrder(order_id,pay_state,state);
        System.out.println(list);
        return getDataTable(list);
    }

    /**
     * 查看订单信息
     */
    @RequiresPermissions("water:order:view")
    @RequestMapping("/showOrder/{id}")
    public String showOrder(@PathVariable("id") String id, ModelMap map){
        //根据Orderid,查找订单信息
        OrderEntity orderEntity = orderService.selectOrderByOrder_id(id);

        map.put("orderEntity",orderEntity);
        //基本信息
        //收货人信息
         int user_id =  orderEntity.getUser_id();
         UserEntity user = userService.selectUserById(user_id);
         map.put("user",user);
        //商品信息
        List<OrderItemEntity> orderItemEntities = orderService.selectOrderItemByOrder_id(id);
        map.put("orderItemEntities",orderItemEntities);
        //操作信息
        return prefix+ "/showOrder";
    }
    @RequiresPermissions("water:order:view")
    @PostMapping("/orderItem/{id}")
    @ResponseBody
    public String orderItem(@PathVariable("id") String order_id){
       // 根据Orderid,查找订单信息
        OrderEntity orderEntity = orderService.selectOrderByOrder_id(order_id);
        System.out.println(orderEntity);
        //基本信息
        //收货人信息
         int user_id =  orderEntity.getUser_id();
         UserEntity userEntity = userService.selectUserById(user_id);
        System.out.println(userEntity);
        //商品信息
        List<OrderItemEntity> orderItemEntity = orderService.selectOrderItemByOrder_id(order_id);
        System.out.println(orderItemEntity);
        Map output = new TreeMap();
        output.put("orderEntity",orderEntity);
        output.put("userEntity",userEntity);
        output.put("orderItemEntity",orderItemEntity);
        JSONObject jsonObject = JSONObject.fromObject(output);
        return jsonObject.toString();
    }

    /**
     * 删除订单
     * @param ids
     * @return
     */
    @PostMapping("/removeOrder")
    @ResponseBody
    public AjaxResult removeOrder (String ids){
//        int num = productService.deleteProductAndNorm(ids);
        int num = orderService.deleteOrder(ids);
        if (num > 0) {
            return success("删除商品成功！！！");
        } else {
            return error("删除商品失败！");
        }
    }
    /**
     *   跳转到订单详情图
     */
    @RequestMapping("/editOrder/{id}")
    public String editImg(@PathVariable("id") int id){
        //根据id，查找商品图片
        return prefix+"/editOrder";
    }
}
